package com.pingan.agentbank.aaos.util;

import com.pingan.agentbank.aaos.exception.AAOSException;
import com.pingan.agentbank.aaos.pojo.Config;
import com.pingan.vframe.logging.Log;
import com.pingan.vframe.logging.LogFactory;
import com.pingan.vframe.util.ClassCreator;
import javassist.CannotCompileException;

public class ClassGenarateUtil {

    private static final Log log = LogFactory.getLog();

    public static LineHandler generateLineHandler(String className,String toServiceDataScript,String toResultStringScript){

        ClassCreator ci = new ClassCreator();

        ci.addImport("com.pingan.agentbank.aaos.pojo.ApproLineInfo");
        ci.addImport("com.pingan.vframe.sdo.ServiceData");
        ci.addImport("com.pingan.vframe.util.JaDateTime");
        ci.addImport("com.pingan.agentbank.aaos.pojo.Config");
        ci.addImport("com.pingan.agentbank.aaos.util.FixedLenUtil");
        ci.addImport("com.pingan.vframe.util.JSON");
        ci.addImport("com.pingan.vframe.util.JSONCaseType");
        ci.addImport("com.pingan.agentbank.aaos.util.WholeToHalfUtil");
        ci.setClassName(className + "ApproLineToServiceData");
        ci.addInterface(LineHandler.class);

        StringBuffer toServiceDataMethod = new StringBuffer();
        toServiceDataMethod.append("public ServiceData toServiceData(ApproLineInfo approLineInfo,ServiceData sourServiceData,ServiceData serviceData) {\n");
        toServiceDataMethod.append(toServiceDataScript);
        toServiceDataMethod.append("}");

        StringBuffer toResultStringMethod = new StringBuffer();
        toResultStringMethod.append("public String toResultString(String approMsg,String fileName,int count,ServiceData serviceData, Config config) {\n");
        toResultStringMethod.append(toResultStringScript);
        toResultStringMethod.append("}");

        ci.addMethod(toServiceDataMethod.toString());
        ci.addMethod(toResultStringMethod.toString());


        LineHandler lineHandler;

        try {
            Class clazz = ci.makeClass();
            lineHandler = (LineHandler) clazz.newInstance();


        } catch (IllegalAccessException e) {
            log.error(e.getMessage());
            throw new AAOSException(e.getMessage());
        } catch (InstantiationException e) {
            log.error(e.getMessage());
            throw new AAOSException(e.getMessage());
        } catch (CannotCompileException e) {
            log.error(e.getMessage());
            throw new AAOSException(e.getMessage());
        }
        return lineHandler;
    }


}
